package com.xuetang9.t9.hotel.mapper;

import com.xuetang9.t9.hotel.beans.EConsumption;
import com.xuetang9.t9.hotel.mapper.condition.ICondition;
import com.xuetang9.t9.hotel.mapper.sql.impl.EConsumptionMapperSql;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @ProjectName: MyHotelProject
 * @Package: com.xuetang9.t9.hotel.mappers
 * @ClassName: IEConsumptionMapper
 * @Author: ChinaHp
 * @Description: 就餐订单
 * @Date: 2020/2/24 15:09
 * @Version: 1.0
 */
public interface IEConsumptionMapper {
    /**
     * 获取当页数据
     * @param site
     * @param size
     * @return
     */
    @Select("select * from e_consumption limit #{site},#{size}")
    List<EConsumption> getDealData(@Param("site") int site, @Param("size") int size);

    /**
     *根据开始和结束获取当页数据
     * @param start
     * @param end
     * @param currPage
     * @param size
     * @return
     */
    @Select("select * from e_consumption where orderTime between #{start} and #{end} limit #{curr},#{size}")
    List<EConsumption> dateFilter(@Param("start") String start,@Param("end") String end ,@Param("curr") int currPage,@Param("size") int size);

    /**
     * 通过id获取当页数据
     * @param id
     * @param currPage
     * @param size
     * @return
     */
    @Select("select * from e_consumption where id = #{id} limit #{curr},#{size}")
    List<EConsumption> idFilter( int id ,@Param("curr") int currPage,int size);

    /**
     * 通过状态获取当页数据
     * @param state
     * @param currPage
     * @param size
     * @return
     */
    @Select("select * from e_consumption where state = #{state} limit #{curr},#{size}")
    List<EConsumption> stateFilter(String state,@Param("curr") int currPage,int size);

    /**
     * 获取数据条数
     * @return
     */
    @Select("select count(0) from e_consumption")
    long dataSize();

    /**
     *获取当天的实付金额
     * @return
     */
    @Select("select sum(actualPrice) from e_consumption where date(checkoutTime) = date(now())")
    Object getTodayIncome();

    /**
     *获取总的实付金额
     * @return
     */
    @Select("select sum(actualPrice) from e_consumption")
    long ljMoney();

    /**
     * 获取已经结账的实付金额
     * @return
     */
    @Select("select sum(actualPrice) from e_consumption where state = '已结账'")
    long jyMoney();

    /**
     * 修改
     * @param eConsumption
     * @return
     */
    @Update("update e_consumption set tablesId = #{tablesId}, orderTime = now(), orderType = '店吃', price = #{price}, state = #{state}, checkoutTime = #{checkoutTime}, actualPrice = #{actualPrice}, period = curdate() where id = #{id}")
    int update(EConsumption eConsumption);

    /**
     * 通过id获取餐饮订单
     * @param id
     * @return
     */
    @Select("select * from e_consumption where id = #{id}")
    EConsumption select(int id);

    /**
     * 通过id获取查询外带的订单
     * @param id
     * @return
     */
    @Select("select * from e_consumption where id = #{id} AND orderType = '外带'")
    EConsumption selectByBesides(int id);

    @Insert("insert into e_consumption(tablesId,staffId , humanCount , orderTime, orderType, price , state, period) value (#{tablesId},#{staffId},1,now(),'店吃',#{price},'待付款',curdate())")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    int insertAfterJoinEat(EConsumption ec);

    @Insert("insert into e_consumption(tablesId,staffId , humanCount , orderTime, orderType, price , state , checkoutTime , actualPrice, period) value (#{tablesId},#{staffId},1,now(),'店吃',#{price},'已付款',now() , #{actualPrice} ,curdate())")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    int insertNowJoinEat(EConsumption ec);

    @Insert("insert into e_consumption(tablesId,staffId , humanCount , orderTime, orderType, price , state,checkoutTime , actualPrice , period) value (#{tablesId},#{staffId},1,now(),'店吃',#{price},'免单', now() , 0 , curdate())")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    int insertFreeJoinEat(EConsumption ec);

    /**
     * 添加店吃订单
     * @param ec
     * @return
     */
    @Insert("insert into e_consumption( tablesId, orderTime, orderType, price, state, checkoutTime, actualPrice, period, comment) VALUE (#{tablesId},now(),'店吃',#{price},#{state},#{checkoutTime},#{actualPrice},curdate(),'无')")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    long insertJoinEat(EConsumption ec);

    /**o
     *添加外吃订单
     * @param eConsumption
     * @return
     */
    @Insert("insert into e_consumption( tablesId, orderTime, orderType, price, state, checkoutTime, actualPrice, period, comment) VALUE (#{tablesId},now(),'外带',#{price},#{state},#{checkoutTime},#{actualPrice},curdate(),'无')")
    long insertOuterEat(EConsumption eConsumption);

    /**
     * 获取尾部id
     * @return
     */
    @Select("select max(id) from e_consumption")
    long selectLastId();


    /**
     * 根据查询条件查询订单数据
     * @param condition
     * @return
     */
    @SelectProvider(value = EConsumptionMapperSql.class,method = "selectByAll")
    @Results(value = {
            @Result(id=true,column = "id",property = "id"),
            @Result(column = "id",property = "orderFoods",many = @Many(select = "com.xuetang9.t9.hotel.mapper.IOrderFoodMapper.selectById"))
    })
    List<EConsumption> selectByAll(@Param("key")ICondition condition);


    /**
     * 根据查询条件查询总数量
     * @param condition
     * @return
     */
    @SelectProvider(value = EConsumptionMapperSql.class,method = "selectByCount")
    Long selectByCount(@Param("key")ICondition condition);

    @Select("select * from e_consumption")
    List<EConsumption> queryAll();

    /**
     * 根据桌位查询正在就餐中的就餐信息
     * @param id
     * @return
     */
    @Select("SELECT * FROM e_consumption WHERE tablesId = #{id} AND diningstate = 0")
    @Results(value = {
            @Result(id=true,column = "id",property = "id"),
            @Result(column = "id",property = "orderFoods",many = @Many(select = "com.xuetang9.t9.hotel.mapper.IOrderFoodMapper.selectById"))
    })
    EConsumption selectByTableId(@Param("id") Integer id);

}
